#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 5e5 + 5;

int n, m;
char s[N];
vector<int> ch[N];
ll ans[N], p[N];
int st[N], top;
int f[N];
void dfs(int u) {
  int pu = 0;
  if (s[u] == '(') {
    st[++top] = u;
  } else {
    if (top) {
      pu = st[top--];
      p[u] = p[f[pu]] + 1;
    }
  }
  ans[u] = ans[f[u]] + p[u];
  for (int v : ch[u]) dfs(v);
  if (pu)
    st[++top] = pu;
  else if (s[u] == '(')
    top--;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  rep(i, 1, n) cin >> s[i];
  rep(i, 2, n) {
    cin >> f[i];
    ch[f[i]].push_back(i);
  }
  dfs(1);
  ll res = 0;
  rep(i, 1, n) res ^= i * ans[i];
  cout << res;
  return 0;
}